package com.like.miaosha.controller;

import com.like.miaosha.domain.User;
import com.like.miaosha.rebbitmq.MQSender;
import com.like.miaosha.redis.RedisService;
import com.like.miaosha.redis.UserKey;
import com.like.miaosha.result.CodeMsg;
import com.like.miaosha.result.Result;
import com.like.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo" )
public class DemoController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MQSender mqSender;
    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        model.addAttribute("name","like");
        return "hello";
    }
    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello(){
        return Result.success("hello like");
    }
    @RequestMapping("/helloError")
    @ResponseBody
    public Result<String> helloError(){
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet(){
        User user=userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
        User v1= redisService.get(UserKey.getById,"key1",User.class);
        System.out.println(v1);
        return Result.success(v1);
    }
    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
        User user=new User(1,"1111");
        boolean ret=redisService.set(UserKey.getById,"key1",user);
        return Result.success(ret);
    }

//    @RequestMapping("/mq")
//    @ResponseBody
//    public Result<String> mq(){
//        mqSender.send("hello,mq");
//        return Result.success("hello word");
//    }
//
//    @RequestMapping("/mq/topic")
//    @ResponseBody
//    public Result<String> topic(){
//        mqSender.sendTopic("hello,mq");
//        return Result.success("hello word");
//    }
//
//    @RequestMapping("/mq/fanout")
//    @ResponseBody
//    public Result<String> fanout(){
//        mqSender.sendFanout("hello,mq");
//        return Result.success("hello word");
//    }
//
//    @RequestMapping("/mq/header")
//    @ResponseBody
//    public Result<String> header(){
//        mqSender.sendHeader("hello,mq");
//        return Result.success("hello word");
//    }
}
